﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class PositionStatusDAL : IPositionStatusDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public PositionStatusDAL()
        {

        }

        public IList<PositionStatusInfo> ListPositionStatus(PositionStatusInfo stInfo)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<PositionStatusInfo> infoList = new List<PositionStatusInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", stInfo.Sequence);

                strSQL = "SELECT STATUS_SEQ, STATUS_NAME, EMPTY"
                     + " FROM HRM_MP_POSITION_STATUSES"
                     + " WHERE RECORD_STATUS not like @DeleteRecord"
                     + (stInfo.Sequence > 0 ? " AND STATUS_SEQ = @Sequence" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        PositionStatusInfo info = new PositionStatusInfo();
                        info.Sequence = Int32.Parse(dr[i]["STATUS_SEQ"].ToString());
                        info.Name = Convert.IsDBNull(dr[i]["STATUS_NAME"]) ? "" : (dr[i]["STATUS_NAME"]).ToString();
                        info.Empty = (dr[i]["EMPTY"]).ToString() == "1";

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            //ARM WRITE

            return infoList;
        }  
    }
}
